<template>
  <div class="shop-cart" >
      <div class="shop-content">
          <div class="shop-left" @click="isshow = !isshow">
              <div class="logo-wrapper">
                  <div class="logo">
                      <i class="el-icon-shopping-cart-2"></i>
                  </div>
                  <div class="num" v-if="totalCount">{{totalCount}}</div>
              </div>
              <div class="totalprice">
                  <div class="price">￥{{totalPrice}}</div>
                  <div class="desc">另需配送费￥{{info.deliveryPrice}}</div>
              </div>
          </div>
          <div class="shop-right">
              <div class="pay" :class="payClass">
                  {{payText}}
            </div>
          </div>
      </div>
      <transition name="move">
          <div class="shop-list" v-if="ListShow">
            <div class="head">
                <span class="title">购物车</span>
                <span class="clear" @click="clearCart">清空</span>
            </div>
            <div class="list">
                <ul>
                    <li class="list-item" v-for="(food,index) in cartFoods" :key="index">
                        <span class="title">{{food.name}}</span>
                        <div class="price">￥{{food.price}}</div>
                        <div class="cartcontrol-wrapper">
                            <cart-control :food="food"></cart-control>
                        </div>
                    </li>
                </ul>
            </div>
        </div>
      </transition>
      <div class="list-mask" v-show="ListShow" @click="toggleShow"></div>
  </div>
</template>

<script>
import BScroll from '@better-scroll/core'
import CartControl from '../CartControl/CartControl.vue'
import {mapState,mapGetters} from 'vuex'
export default {
    data () {
        return {
            isshow:false,
        }
    },
    components:{
        CartControl,
    },
    computed:{
        ...mapState(['cartFoods', 'info']),
        ...mapGetters(['totalCount','totalPrice']),
        payClass(){
            const {totalPrice} = this
            const {minPrice} = this.info
            return totalPrice >= minPrice ? 'enough' : 'not-enough'
        },
        payText(){
            const {totalPrice} = this
            const {minPrice} = this.info
            if(totalPrice === 0){
                return `￥${minPrice}元起送`
            }else if(totalPrice < minPrice){
                return `还差${minPrice-totalPrice}元起送`
            }else{
                return '结算'
            }
        },
        ListShow(){
            if(this.totalCount === 0){
                this.isshow = false
                return false
            }
            if(this.isshow){
                this.$nextTick(() => {
                    setTimeout(()=> {
                        if(!this.scroll){
                        this.scroll = new BScroll('.list', {
                            click:true
                        })
                        }else{
                            this.scroll.refresh() // 让滚动条刷新一下: 重新统计内容的高度
                        }
                    },500)
                })
            }
            return this.isshow
        }
    },
    methods:{
        toggleShow(){
            if(this.totalCount > 0){
                this.isshow = !this.isshow
            }
        },
        clearCart(){
            this.$store.dispatch('clearCart')
        }
    }
}
</script>

<style lang="less" scoped>
.shop-cart{
    position: fixed;
    left: 0;
    bottom: 0;
    z-index: 50;
    width: 100%;
    height: 48px;
    .shop-content{
        display: flex;
        background: #141d27;
        color: rgba(255, 255, 255, 0.4);
        .shop-left{
            flex: 1;
            display: flex;
            .logo-wrapper{
                display: inline-block;
                vertical-align: top;
                position: relative;
                top: -10px;
                margin: 0 12px;
                padding: 6px;
                width: 56px;
                height: 56px;
                box-sizing: border-box;
                border-radius: 50%;
                background: #141d27;
                .logo{
                    width: 100%;
                    height: 100%;
                    border-radius: 50%;
                    text-align: center;
                    background: #029b77;
                    font-size: 40px;
                    color: #fff;
                }
                .num{
                    position: absolute;
                    top: 0;
                    right: 0;
                    width: 24px;
                    height: 16px;
                    line-height: 16px;
                    font-size: 9px;
                    text-align: center;
                    border-radius: 16px;
                    font-weight: 700;
                    color: #ffffff;
                    background: rgb(240, 20, 20);
                    box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.4)
                }
            }
            .totalprice{
                .price{
                    display: inline-block;
                    vertical-align: top;
                    margin-top: 5px;
                    line-height: 24px;
                    padding-right: 12px;
                    box-sizing: border-box;
                    font-size: 16px;
                    font-weight: 700;
                    color: #fff;
                }
                .desc{
                    font-size: 13px;
                    color: rgb(160, 158, 158);
                }
            }
        }
        .shop-right{
            flex: 0 0 105px;
            width: 105px;
            .pay{
                height: 48px;
                line-height: 48px;
                text-align: center;
                font-size: 12px;
                font-weight: 700;
                color: #fff;
                background: #029b77;
            }
        }
    }
    .shop-list{
        position: absolute;
        left: 0;
        top: 0;
        z-index: -1;
        width: 100%;
        transform: translateY(-100%);
        &.move-enter-active,&.move-leave-active{
            transition: transform .3s;
        }
        &.move-enter,&.move-leave-to{
            transform: translateY(0);
        }
        .head{
            height: 40px;
            line-height: 40px;
            background: #f3f5f7;
            display: flex;
            justify-content: space-between;
            padding: 0 10px;
            .title{
                font-size: 14px;
                color: rgb(7, 17, 27);
            }
            .clear{
                font-size: 14px;
                color: #029b77;
            }
        }
        .list{
            padding: 0 18px;
            max-height: 217px;
            overflow: hidden;
            background: #fff;
            .list-item{
                position: relative;
                border-bottom: 1px solid lightgray;
                padding: 12px 0;
                box-sizing: border-box;
                .title{
                    line-height: 24px;
                    font-size: 14px;
                    
                    color: rgb(7, 17, 27);
                }
                .price{
                    position: absolute;
                    right: 90px;
                    bottom: 12px;
                    line-height: 24px;
                    font-size: 14px;
                    color: red;
                }
                .cartcontrol-wrapper{
                    position: absolute;
                    right: 0;
                    bottom: 6px;
                }
            
            }
        }
    }
    .list-mask{
        position: fixed;
        height: 100%;
        width: 100%;
        top: 0;
        left: 0;
        z-index: -2;
        opacity: 1;
        background: rgba(7, 17, 27, 0.6)
    }
}
</style>